package com.rgs.se.concurrent;

import java.util.concurrent.CountDownLatch;

/**
 * @author Ren Gaoshuai
 * @ClassName TestCountDownLatch
 * @Description CountDownLatch的使用，可以指定线程的顺序
 * @date 2021-10-27 15:27
 **/
public class TestCountDownLatch {

    private static CountDownLatch countDownLatch = new CountDownLatch(3);

    public static void main(String[] args) {
        new Thread(){
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"线程正在执行");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"线程执行完毕");
                countDownLatch.countDown();
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"线程正在执行");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"线程执行完毕");
                countDownLatch.countDown();
            }
        }.start();

        new Thread(){

            @Override
            public void run() {
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"线程正在执行");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"线程执行完毕");

            }
        }.start();


//        try {
//            countDownLatch.await();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        System.out.println("执行主线程！");
        countDownLatch.countDown();
    }
}
